package ro.k.jstore;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import ro.k.jstore.beans.ManoperaBaseJBean;
import ro.k.web.beans.order.ManoperaBaseBean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ManoperaBaseService {

	private static final String file = "c:\\manopera.json";

	private static ManoperaBaseService inst = null;
	private List<ManoperaBaseJBean> mans = null;

	public static ManoperaBaseService getInstance() {
		if (inst == null) {
			inst = new ManoperaBaseService();
		}
		return inst;
	}

	private List<ManoperaBaseJBean> loadManoperaBase() {
		if (mans == null) {
			Gson gson = new Gson();
			try {
				BufferedReader in = new BufferedReader(new FileReader(file));
				Type type = new TypeToken<List<ManoperaBaseJBean>>() {
				}.getType();

				mans = gson.fromJson(in, type);

				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return mans;
	}

	public List<ManoperaBaseBean> getManoperaBase() {
		List<ManoperaBaseJBean> fromFile = loadManoperaBase();
		List<ManoperaBaseBean> beans = new ArrayList<ManoperaBaseBean>(
				fromFile.size());

		for (ManoperaBaseJBean ff : fromFile) {
			beans.add(new ManoperaBaseBean(ff.getNume(), ff.getCod(), ff
					.getValoare()));
		}

		return beans;
	}
}
